/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package info.client;

import java.io.*;
import java.net.*;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
/**
 *
 * @author mark
 */
public class SocketConnection {
    //Attributen
    private static InetAddress host;
    private static Socket socket;
    private String query;
    private ObjectInputStream ois;
    //Constructor
    public SocketConnection(){
        try {
            host = InetAddress.getLocalHost();
            socket = new Socket(host.getHostName(), 4444);
            
        } catch(Exception e) {
          e.getMessage();  
        }
    }
    
    public static Socket createSocket()
    {
        try {
            socket = new Socket(host.getHostName(), 4444);
        } catch (Exception e) {
            Logger.getLogger(SocketConnection.class.getName()).log(Level.SEVERE, null, e);
        }
        return socket;
    }
            
    
    //Methode om query over socket te verzenden
    public void send(String query){
        this.query = query;
        createSocket();
        try {
            ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
            oos.writeObject(query);
        } catch ( IOException ex ){
        }
       
    }
    //Methode om resultaten van de query van de SocketConnectie te lezen.
    public Object[][] read(){
        Object[][] result = null;

            try{
            ois = new ObjectInputStream(socket.getInputStream());
            result =  (Object[][]) ois.readObject();
            
//                if(result[0][0] == "invalid")
//                {
//                    JOptionPane.showMessageDialog(null,"Query is onjuist","Mislukt",JOptionPane.WARNING_MESSAGE);
//                }
            }

             catch(Exception e) {
                e.getMessage();
                //JOptionPane.showMessageDialog(null,"Query is onjuist","Mislukt",JOptionPane.WARNING_MESSAGE);
            }

        return result;
        
    
    }
    
}